package com.tyj.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.tyj.gulimall.order.vo.payVo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117627848";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDWTexyaqavaAYRYKsvP2xp8szc3XmCY6TOQPHzWtxqFJbjhA6Ya3nlXhzK3MqMd7DtWtEBanQ6vZHq8BC5tV39qmoz4Qh1RUiR5IpY7eRfJsMqGeRyKrvFHVX2C55s3MXXyez/8hfnUkVd+/DMrfFtWDEFXQHBCvbG5/JPNlhjub/odV6YVK8p5h1yRcr2z4dfOLBnhbs4fw23KkigmCissh8XQqpmiWgbXkIuOZeg0aGod+weXlL4739URwLIviSJg4cVw+7VZHINUU1TDzs8G//ORUkgeZEyxuoVJgO9Quum4h3td27FaZgUUNK6xkZQmEEiKIHi56CNhqoV/5T7AgMBAAECggEAHwkL7An7970V2+BnCiGiPrm152p/lV1+2YvYPkjzUL2ImIxgXLx727bEbrCI+MzAp68WQ6E6EeOwRRjdIzfb08ehN8AoLGuLNe2mB3j0ML95VhiH1+zOYp57+3T1TYivci/BDdPumEZ220SIO+wY4Z/7AW7VuTIwSOAAXG5fYJmbFGsRYAkX1BVA1d1wZoBnZJ1/McCeAtnhb6tBYyfOtdBnuTYj4sgpoFcrEkSCd24SGh6D1vr7zpnUfcg+cZmTssThwdfTsjbtwsdHOpboaBsXCuldpIPb46qvW1fQ+vAIDyFJBYufwVpRDquMbarBr5l0Wv0K5IBuqrXt58jpkQKBgQDroDQdT6y74tr4EGXwgsW2KQlEl64LkOashbbYUh79NdBUC5/kwW9fConLFQHTKeg041/FYpsU2ouOmyPj4t0yf1fvW/4G9CWxAXxRJWCnymwFtMZFW+XLbaeL9FpsqXbQnFyBQl3wZm9K8gRk7DmM1x71TpWVq7iVgQAmJ/oe8wKBgQDo1cA43Y+Y8MFE9uR1YyiYnziEIn4djoNp+LVOGAJ+F7fqqwXCknZMfAafrlXgWytOLW+ZP0KrIKORJED5P3twNbBdvca6Nq2xwq4rvDrqig66lAzSycvkbI4QXUp2SO8COL6FUHrpZnKCMKiljdYwcUUvjAEHhkJ1iL8kUYeD2QKBgQDfJmsUs2dgzXkZY9ZXUFJUsUqAtrHzbDi6fSxsdGG65uM1JYk5iWSjewSC25NN3Ct5IzmJY1iCCHQRC6aS5UEd2PJ70wCir0ie+Uw2QZLnddKWGN2JxH2+CkyaREH7FQdcjCkjlaoHyctvDAPTXNEoLHe6VMFf7TC0DmcIu+WqtwKBgDLN0S+XSKn6fnFp3devV0BWHaLXU+VnH02UMxtjKcgRrNPLlWorQxzVQyy8gYh+3Cgmyd8KaqWkbYxR+MtqCqklTeJmcmJXY/cRn6dzXvemQCh9/X5RAehJt4XilUjSCTTW5Nt6ABfFft7hdysIk9/mF06UofTeWsoCmJJ4J7WhAoGBAJIWNTNgeLTDW5Jyc5VUjKY5TxRo4yNk91iHbTbPIhJhUm5zQSBRw+Zwl6P64q0znwANXFVv+o04bWVO1ieeERrIqyxwBU6GkEWFxC2vuaXe0RJYD9xyehu+DbG0ZqSkYt10O1qrNWxJMBlpJJYmjIaL1rX6J34H4XosoAtCzDKF";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA22XOpS+KuZYCwH9/1BCM/E5HES1wyZYnR1i4+v1TlP3R8Kt0QnZTbXoHfmaPN+SWAHyC+W9PxQddn54d6iLRqy8IYliWu/7yvjQU6KF9At+Vouqo+Qa579t9+VvnCwxOS+CkwM8R/ai30XVTUM8JMURo3eWU3aKOZjfj7r1MIYXA85P/6ZD5rbl82K+kW2eBORaWiffZMrSg8oyJterBi4YU2udvKdQFWbpuo6WEH5u34L30/fMgBo5vBIxvTyBqbIC4LHYKQcu0WJ49sRjBs8KDrTZCWdrHAC9+67IYL6noh4KaaCh/w6r+mQcA9MglrD7kp2y3szolKXPoR5VKuwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://6d9wykzmxq.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","  //订单支付未超过一分钟 支付超时
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
